package com.abbas.abchat.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.abbas.abchat.R;
import com.abbas.abchat.dao.AccountDao;
import com.abbas.abchat.dao.impl.AccountDaoImpl;
import com.abbas.abchat.db.Sqlite;
import com.abbas.abchat.entity.Account;
import com.abbas.abchat.utils.SqlUtils;
import com.abbas.abchat.utils.TextLengthUtils;

import org.w3c.dom.Text;

import java.util.HashMap;
import java.util.Map;

public class RegisterActivity extends BaseActivity {

    private EditText etAccount;
    private EditText etPassword;
    private Button btRegister;

    private AccountDao accountDao;

    @Override
    protected void init() {
        etAccount = (EditText) findViewById(R.id.et_account);
        etPassword = (EditText) findViewById(R.id.et_password);
        btRegister = (Button) findViewById(R.id.bt_register);

        accountDao = new AccountDaoImpl(this);
    }

    @Override
    protected void initEvent() {
        btRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String account = etAccount.getText().toString().trim();
                String password = etPassword.getText().toString().trim();

                if (!TextUtils.isEmpty(account) && !TextUtils.isEmpty(password) &&
                        TextLengthUtils.isOverTo(account,TextLengthUtils.APMinLength) && TextLengthUtils.isOverTo(password,TextLengthUtils.APMinLength) &&
                        !TextLengthUtils.isLessOf(account, TextLengthUtils.APMaxLength) && !TextLengthUtils.isLessOf(password, TextLengthUtils.APMaxLength)){
                    //即将注册的账号与密码
                    Account entity = new Account(account,password);

                    try {
                        //开始插入
                        long resultCode = accountDao.insert(entity);
                        //插入失败
                        if (resultCode == -1){
                            showMessage(getResources().getString(R.string.registerFail));
                        }else {
                            //TODO 插入成功 创建个人关系表
                            SqlUtils.getInstance(RegisterActivity.this).createRelationTable(resultCode);

                            entity.setId((int) resultCode);
                            entity.setChatTableDbName(SqlUtils.RELATION_TABLE_HEADER + resultCode);

                            //写入当前注册账号的关系表字段
                            int result = accountDao.updateById(entity);

                            if (result <= 0){
                                showMessage("未知错误");
                            }else {
                                showMessage(getResources().getString(R.string.registerSuccess));
                            }
                            finishThis();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }else {
                    showMessage(getResources().getString(R.string.apFormatError));
                }
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_register;
    }
}